package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.movecar.R;
import com.movecar.utils.DataStore;

/**
 * @author Zhao.JQ
 * 
 */
public class WelcomeActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcom_activity);
		initView();
	}

	private void initView() {
		SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
		//
		String userId = sharedPreferences.getString("userId", "");
		String mobile = sharedPreferences.getString("mobile", "");
		if (!"".equals(userId) && !"".equals(mobile)) {
			DataStore.getInstance().setMobile(mobile);
			DataStore.getInstance().setUserId(userId);
			startActivity(new Intent(this, StartActivity.class));
			finish();
		}
		ViewPager paper = (ViewPager) findViewById(R.id.viewpager);
		List<View> mListViews = new ArrayList<View>();

		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.welcome_view1, null);
		mListViews.add(view);
		view = inflater.inflate(R.layout.welcome_view2, null);
		mListViews.add(view);
		view = inflater.inflate(R.layout.welcome_view3, null);

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(WelcomeActivity.this, PhoneLoginActivity.class);
				startActivity(intent);
				finish();

			}
		});
		mListViews.add(view);

		paper.setAdapter(new MyViewPagerAdapter(mListViews));
	}

	public class MyViewPagerAdapter extends PagerAdapter {
		private List<View> mListViews;

		public MyViewPagerAdapter(List<View> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mListViews.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mListViews.get(position), 0);
			return mListViews.get(position);
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

}